package com.xpgk.mvc.statistic.daySales;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.xpgk.annotation.Service;
import com.xpgk.mvc.base.BaseService;
import com.xpgk.mvc.codeSet.Code;

@Service(name = DaySalesStatisticService.serviceName)
public class DaySalesStatisticService extends BaseService{

	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(DaySalesStatisticService.class);
	
	public static final String serviceName = "daySalesStatisticService";
	
	public List<Record> getStatisticData(String date, String organIds){
		List<Record> result = new ArrayList<>();
		if(!"-1".equals(date) && !"-1".equals(organIds)){
				int hjdlcrs=0;//合计单疗程人数
				double hjdlcje=0;//合计单疗程金额
				int hjbzlcrs=0;//合计标准疗程人数
				int hjbzlclcs=0;//合计标准疗程疗程数
				double hjbzlcje=0;//合计标准疗程金额
				int hjzlcrs=0;//合计转疗程人数
				int hjzlclcs=0;//合计转疗程疗程数
				double hjzlcje=0;//合计转疗程金额
				double hjmrjzje=0;//合计每日减重金额
				double hjmrproje=0;//合计每日产品金额
				String sql_allcustomer = getSqlMy(DaySalesStatisticSQL.sqlId_allCustomer);
				List<Record> customers = Db.find(sql_allcustomer, organIds, date);
				if(customers.size() > 0){
					for(Record customer : customers){
						String sql_customerSales = getSqlMy(DaySalesStatisticSQL.sqlId_customerSales);
						List<Record> cus_sales = Db.find(sql_customerSales, customer.getStr("customerID"), organIds, date);
						if(cus_sales.size() > 0){
							double lctotalprice=0;//减重 金额
							double producttotalprice=0;//产品金额
							for(Record sales : cus_sales){
								
								//单疗程
								if(sales.getStr("xflb").equals("xflbgm") && sales.getStr("type").equals(Code.lc_ids) && sales.getStr("units").equals(Code.dlc_units)){
									customer.set("singleLc_peopleNum", 1);
									customer.set("singleLc_unitPrice", sales.getFloat("unitPrice"));
									customer.set("singleLc_totalPrice", sales.getDouble("totalPrice"));
									lctotalprice+=sales.getDouble("totalPrice")==null?0:sales.getDouble("totalPrice");
									hjdlcrs++;
									hjdlcje+=sales.getDouble("totalPrice")==null?0:sales.getDouble("totalPrice");
								}
								
								//标准疗程
								if(sales.getStr("xflb").equals("xflbgm") && sales.getStr("type").equals(Code.lc_ids) && sales.getStr("units").equals(Code.bzlc_units)){
									customer.set("normLc_peopleNum", 1);
									customer.set("normLc_lcNum", sales.getBigDecimal("num"));
									customer.set("normLc_unitPrice", sales.getFloat("unitPrice"));
									customer.set("normLc_totalPrice", sales.getDouble("totalPrice"));
									lctotalprice+=sales.getDouble("totalPrice")==null?0:sales.getDouble("totalPrice");
									hjbzlcrs++;
									hjbzlclcs+=sales.getBigDecimal("num")==null?0:Integer.parseInt(sales.getBigDecimal("num").toString());
									hjbzlcje+=sales.getDouble("totalPrice")==null?0:sales.getDouble("totalPrice");
								}
								
								//转疗程
								if(sales.getStr("xflb").equals("xflbzx") && sales.getStr("type").equals(Code.lc_ids)){
									customer.set("upLc_peopleNum", 1);
									customer.set("upLc_lcNum", sales.getBigDecimal("num"));
									customer.set("upLc_totalPrice", sales.getFloat("receivable"));
									lctotalprice+=sales.getDouble("totalPrice")==null?0:sales.getDouble("totalPrice");
									hjzlcrs++;
									hjzlclcs+=sales.getBigDecimal("num")==null?0:Integer.parseInt(sales.getBigDecimal("num").toString());
									hjzlcje+=sales.getFloat("receivable")==null?0:sales.getFloat("receivable");
								}
								
								//其他产品
								if(sales.getStr("xflb").equals("xflbgm") && !sales.getStr("type").equals(Code.lc_ids)){
									customer.set(sales.getStr("bh") + "_num", sales.getBigDecimal("num"));
									customer.set(sales.getStr("bh") + "_price", sales.getDouble("totalPrice"));
									producttotalprice += sales.getDouble("totalPrice");
								}
								
								
								customer.set("lctotalprice", lctotalprice);
								customer.set("producttotalprice", producttotalprice);
								customer.set("sum", producttotalprice+lctotalprice);
							}
							hjmrjzje+=lctotalprice;
							hjmrproje+=producttotalprice;
							//是否新增
							if(customer.getStr("intotime").equals(customer.getStr("date")))
							{
								customer.set("newadd", "是");
							}else
							{
								customer.set("newadd", "否");
							}
							
						}
						
						result.add(customer);
					}
				}
				Record  hj=new Record();
				hj.set("date", "合计");
				hj.set("singleLc_peopleNum", hjdlcrs);
				hj.set("singleLc_totalPrice", hjdlcje);
				hj.set("normLc_peopleNum", hjbzlcrs);
				hj.set("normLc_lcNum", hjbzlclcs);
				hj.set("normLc_totalPrice", hjbzlcje);
				hj.set("upLc_peopleNum", hjzlcrs);
				hj.set("upLc_lcNum", hjzlclcs);
				hj.set("upLc_totalPrice", hjzlcje);
				hj.set("lctotalprice", hjmrjzje);
				String sql_product = getSqlMy(DaySalesStatisticSQL.sqlId_product);
				List<Record> products = Db.find(sql_product, Code.lc_ids, Code.proYt_ids);
				for(Record product:products)
				{	
					double  hjprosl=0;
					double  hjproje=0;
					for(int n=0;n<result.size();n++)
					{
						hjprosl+=result.get(n).getBigDecimal(product.getStr("bh") + "_num")==null?0:Double.parseDouble(result.get(n).getBigDecimal(product.getStr("bh") + "_num").toString());
						hjproje+=result.get(n).getDouble(product.getStr("bh") + "_price")==null?0:result.get(n).getDouble(product.getStr("bh") + "_price");
					}
					hj.set(product.getStr("bh") + "_num", hjprosl);
					hj.set(product.getStr("bh") + "_price", hjproje);
				}
				hj.set("producttotalprice", hjmrproje);
				hj.set("sum", hjmrproje+hjmrjzje);
				if(result.size()>0)
				{
					result.add(hj);
				}
		}
		return result;
	}

}
